package com.example.httpclinetdemo;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edt_url;
    private Button btn_obtainContent;
    private TextView tv_content;
    private String url_str;
    private String result;
    private static final String TAG = "MainActivity";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Log.e(TAG, "url_str: " + url_str);
                    if (!TextUtils.isEmpty(result)) {
                        tv_content.setText(result);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iniView();


    }

    private void iniView() {
        edt_url = (EditText) findViewById(R.id.edtTxt_inputwersite);
        btn_obtainContent = (Button) findViewById(R.id.btn_obtaincontent);
        tv_content = (TextView) findViewById(R.id.tv_content);
        btn_obtainContent.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        url_str = edt_url.getText().toString().trim();
        tv_content.setText("");
       /* new Thread(new Runnable() {
            @Override
            public void run() {
                result = getResult(url_str);
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        }).start();*/

        //new MyAsyncTask().execute();
        new Thread(new Runnable() {
            @Override
            public void run() {
                result = getResult(url_str);
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (!TextUtils.isEmpty(result)) {
                            tv_content.setText(result);
                        }
                    }
                });
            }
        }).start();
    }


    class MyAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... strings) {
            String results = "";
            results = getResult(url_str);
            return results;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            tv_content.setText(s);
        }
    }


    private String getResult(String urlstr) {
        String resopnse = "";
        try {
            URL url = new URL(url_str);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(1000 * 10);
            connection.setRequestMethod("GET");
            int code = connection.getResponseCode();
            Log.e(TAG, "code: " + code);
            if (code == 200) {
                InputStream in = connection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                StringBuffer sb = new StringBuffer();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                }
                resopnse = sb.toString();
                in.close();
                bufferedReader.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resopnse;
    }
}
